<?php
	namespace Model;
	use Think\Model;
	class GameRoleModel extends Model{
        protected $tableName = 'question';

        public static function get_question(){
            $model=new \Model\GameRoleModel();
            $count=$model->count();
            $question=$model->field("uid,title,tip")->where("uid=".mt_rand(1,$count))->find();
//            var_dump($question);
            return $question;
        }
        //加分,然后修改paint_uid,now_term,$answers为用户uid的数组
        public static function add_point($room_uid,$answers=array()){
            $db=new \Model\DbModel();
            $paint_uid=D('room')->field('paint_uid')->where("uid=".$room_uid)->find()['paint_uid'];
//            D("user")->save(array("uid"=>$paint_uid,"point"=>12));
            $count=count($answers);
//            $db->startTrans();//开启事务
            //几个人答对，画画者就几分
            $db->updatedataforint("user",array("point"=>"point+".$count),"uid=".$paint_uid);
            //第一个答对的加3分，第二个加2分，其余的加一分
            if($count>=1)
                $db->updatedataforint("user",array("point"=>"point+"."3"),"uid=".$answers[0]);
            if($count>=2)
                $db->updatedataforint("user",array("point"=>"point+"."2"),"uid=".$answers[1]);
            //其他用户加1分
            if($count>=3){
                unset($answers[0]);
                unset($answers[1]);
                $answers=implode(",",$answers);
                $db->updatedataforint("user",array("point"=>"point+"."1"),"uid in(".$answers.")");
            }
//            $db->rollback();//失败，事务回滚
//            $db->commit();//成功，提交事务
//            var_dump($paint_uid) ;

        }
	}
?>